<?php

namespace App\Http\Controllers\Category;

use App\Models\Attr;
use App\Models\Category;
use App\Models\CategoryAttr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categorys=Category::paginate(10);
        return view('category.index',compact('categorys'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 获取所有分类数据
        $categorys=Category::get();
        // 获取所有顶级属性
        $attrs=Attr::where('pid',0)->get();
        return view('category.create',compact('categorys','attrs'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $post=$request->all();
//        dd($post);
        // 添加数据后返回当前category数据
        $category=Category::create($post);

        // 判断分类数据是否勾选属性
        if(isset($post['attrs'])){
            foreach ($post['attrs'] as $k=>$v){
                CategoryAttr::create(
                    [
                        'category_id'=>$category['id'],
                        'attr_id'=>$v
                    ]
                );
            }
        }

        return redirect()->route('category.category.index')->with('success','分类添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category)
    {
        // 获取所有分类数据
        $categorys=Category::get();
        // 获取所有顶级属性
        $attrs=Attr::where('pid',0)->get();
        // 获取当前分类可用属性id
        $attrIds = CategoryAttr::where('category_id', $category['id'])->pluck('attr_id');
        return view('category.edit',compact('category','categorys','attrs','attrIds'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Category $category)
    {
        // 更新当前所有数据
        $category->update($request->all());
        // 判断是否选中属性
        if(isset($request->attrs)){
            // 先删除此条分类关联属性
            CategoryAttr::where('category_id', $category['id'])->delete();
            // 更新新的属性
            foreach ($request->attrs as $k=>$v){
                CategoryAttr::create(
                    [
                        'category_id'=>$category['id'],
                        'attr_id'=>$v
                    ]
                );
            }
        }
        return redirect()->route('category.category.index')->with('success','分类编辑成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category)
    {
        // 检测当前需要删除的分类是否有子分类,如果有,不让删除
        if ($category->hasChild($category)) {
            return back()->with('error', '请先删除当前分类的子分类!');
        }
        // 删除分类属性表中当前分类的可用属性
        CategoryAttr::where('category_id', $category['id'])->delete();
        // 如果没有子分类,直接删除当前分类
        $category->delete();
        // 提示信息并返回
        return back()->with('success', '删除成功');
    }

    // 获取当前数据的当前子集
    public function getSon(Category $category){
        $son=Category::where('pid',$category['id'])->get();
        return $son;
    }

    // 获取分类数据对应的属性
    public function getAttrs(Category $category){
        // 去分类属性表中找到当前分类id的属性id
        $ids=CategoryAttr::where('category_id',$category['id'])->pluck('attr_id');
        // 去属性表中找到id对应的数据
        $attrs=Attr::whereIn('id',$ids)->get();
        return $attrs;
    }
}
